<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
	xmlns:cs="http://myfaces.apress.com/custom"
	template="/WEB-INF/layouts/standard.xhtml" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich"
	xmlns:bf="http://www.bssmanager.com/jsf" xmlns:c="http://java.sun.com/jstl/core"
	xmlns:fn="http://java.sun.com/jsp/jstl/functions">

	<ui:define name="headIncludes">
		<script>
			function checkOutConfirmation(pn) {
				return showConfirmation("Please make a note that you need to refund "+									
									"\nSecurity Deposit and Utensil Deposit " +
									"\nafter clicking the OK button to complete the process !"+
									"\nAre you sure to start the check-out for the patient "+pn+" ? ");
			}				
		</script>
		<style>
			td {
				vertical-align: top;
			}
		</style>
	</ui:define>
	
	<ui:define name="formheader">
		<h2>Check Out</h2>
	</ui:define>
	
	<ui:define name="content" >
		<h:form id="exitForm"   >
			<a4j:status forceId="true" id="chkout">
							<f:facet name="start">
								<h:graphicImage value="/images/im1.gif" />
							</f:facet>
						</a4j:status>
						
			<rich:panel style="border:0">
				<f:facet name="header">
					<h:outputText value="Select Patient to checkout"/>
				</f:facet>
				<h:panelGroup>
					<bf:admissionSelector controller="#{admissionController}" selectMethod="selectAdmission" reRender="selectedPatient,checkOutWindow"/>
				</h:panelGroup>			
			</rich:panel>
			
			<rich:panel style="border:0">
				<f:facet name="header">
					<h:outputText value="Patient Details"/>
				</f:facet>
				<h:panelGroup id="selectedPatient" style="display:#{admissionController.exitableAdmission.id gt 0 ? 'block':'none' }">
					<h:panelGrid columns="3">
					<h:panelGrid columns="1">
						<h:graphicImage url="/bss/images/saved?id=#{admissionController.exitableAdmission.patient.id}" onmouseout="hideLarge()"
									style="border-width: 1px; border-style:solid; border-color:black;"/>						
						<h:outputText value="#{admissionController.exitableAdmission.patient.name} (#{admissionController.exitableAdmission.id})" />
															
																	<h:panelGrid columns="2">
																		<f:facet name="header">
																			<h:outputLabel value="Deposits" />
																		</f:facet>
																		<h:outputText value="Security: " />
																		<h:outputText value="#{admissionController.securityDeposit}"/>
																		<h:outputText value="Utensil: " />
																		<h:outputText value="#{admissionController.utensilDeposit}"/>
																	</h:panelGrid>
					</h:panelGrid>	
					<h:panelGrid columns="1">						
							<h:panelGroup >
							<h:outputLabel value="Checked In: " />
							<h:outputText value="#{admissionController.exitableAdmission.dateOfAdmission}">
								<f:convertDateTime pattern="dd MMM yyyy HH:MM" timeZone="#{preferences.timeZone.ID}" />
							</h:outputText>	
							</h:panelGroup>
													<h:panelGroup>
														<rich:dataList value="#{admissionController.exitableAdmission.currentAttendents}" 
																		var="aBean" rowKeyVar="ii" style="line-height:100%">
															<f:facet name="header">
																<h:outputLabel value="Attendents" />
															</f:facet>
															<h:outputText value="#{ii+1})  #{aBean.attendent.name}" />															
														</rich:dataList>
														<h:outputText rendered="#{empty admissionController.exitableAdmission.currentAttendents}" value="No Attendents" />
													</h:panelGroup>
													
																<h:panelGrid columns="1">
																	<f:facet name="header">
																		<h:outputLabel value="Rooms" />
																	</f:facet>
																	<c:forEach items="#{admissionController.exitableAdmission.roomBookings}" var="roomBooking">
																		<h:outputText value="#{roomBooking.room.roomNumber} ( #{roomBooking.room.building.name} )" />
																		
																			
																		<h:panelGroup >
																			<h:outputText value="From "/>
																			<h:outputText value="#{roomBooking.checkInDate}">
																				<f:convertDateTime pattern="dd MMM yyyy HH:MM" timeZone="#{preferences.timeZone.ID}" />
																			</h:outputText>
																			<h:outputText value=" to "/>
																			
																				
																				<h:outputText rendered="#{not empty roomBooking.checkOutDate}" value="#{roomBooking.checkOutDate}">
																					<f:convertDateTime pattern="dd MMM yyyy HH:MM" timeZone="#{preferences.timeZone.ID}" />
																				</h:outputText>
																			
																			
																				<h:outputText rendered="#{empty roomBooking.checkOutDate}" value=" till date"/>
																			
																			
																		</h:panelGroup>
																	</c:forEach>
																	<c:if test="#{empty admissionController.exitableAdmission.roomBookings}">
																		<h:outputText value="No Rooms Booked" />
																	</c:if>
																</h:panelGrid>
																<h:panelGrid columns="1">
																	<f:facet name="header">
																		<h:outputLabel value="Dormitory Beds: " />
																	</f:facet>
																	<c:forEach items="#{admissionController.exitableAdmission.bedBookings}" var="bedBooking">
																		<h:outputText
																			value="#{bedBooking.bed.bedNumber} ( #{bedBooking.bed.room.building.name} - #{bedBooking.bed.room.roomNumber} )" />
																		<h:panelGroup>
																			<h:outputText value="From "/>
																			<h:outputText value="#{bedBooking.checkInDate}">
																				<f:convertDateTime pattern="dd MMM yyyy HH:MM" timeZone="#{preferences.timeZone.ID}"  />
																			</h:outputText>
																			<h:outputText value=" to "/>
																			<h:outputText rendered="#{not empty bedBooking.checkOutDate}" value="#{bedBooking.checkOutDate}">
																				<f:convertDateTime pattern="dd MMM yyyy HH:MM" timeZone="#{preferences.timeZone.ID}"  />
																			</h:outputText>	
																			<h:outputText rendered="#{ empty bedBooking.checkOutDate}" value=" till date"/>
																		</h:panelGroup>
																	</c:forEach>
																	<c:if test="#{empty admissionController.exitableAdmission.bedBookings}">
																		<h:outputText value="No Beds Booked" />
																	</c:if>
																</h:panelGrid>	
																<h:panelGrid columns="4">
																	<f:facet name="header">
																		<h:outputLabel value="Material Set" />
																	</f:facet>
																	<c:forEach items="#{admissionController.exitableAdmission.materialAllocations}" var="materialAllocation">
																		<h:outputLabel value="#{materialAllocation.materialType.name}" />
																		<h:outputText value="#{materialAllocation.instances}"
																			style="width: 15px; margin-right:15px; text-align: center;" />
																	</c:forEach>
																</h:panelGrid>
																<h:panelGrid columns="2">
																	<h:panelGrid>
																		<f:facet name="header">
																			<h:outputLabel value="Utensil Set" />
																		</f:facet>
																		<h:outputText value="#{admissionController.exitableAdmission.materialSet}" converter="#{materialSetConverter}"/>
																	</h:panelGrid>
																</h:panelGrid>	
							
					</h:panelGrid>	
					<h:panelGroup>
						<h:commandButton	onclick="if(!checkOutConfirmation('#{admissionController.exitableAdmission.patient.firstName} #{admissionController.exitableAdmission.patient.lastName} #{admissionController.exitableAdmission.patient.middleName}')) return false; Richfaces.showModalPanel('checkOutWindow'); return false;"
											title="Click to start check out"
											image="/images/log-out.png"											
											style="width:50px;height:57px;"																						
											styleClass="norender" />						
						</h:panelGroup>
					</h:panelGrid>												
				</h:panelGroup>		
			</rich:panel>					
			
						<rich:dataTable width="100%" value="#{admissionController.exitedAdmissions}" var="aBean" rowKeyVar="rowVar" id="exitedAdmissions">
							<f:facet name="header">
								<h:outputText value="Patients checked out since yesterday" />
							</f:facet>
							<rich:column>
								<f:facet name="header">S/N</f:facet>
								<h:outputText value="#{rowVar+1}" />
							</rich:column>
							<rich:column>
								<f:facet name="header">ID</f:facet>
								<h:outputText value="#{aBean.id}" />
							</rich:column>																																				
							<rich:column style="text-align:center">
								<f:facet name="header">Photograph</f:facet>
								<h:graphicImage width="50" height="37" url="/bss/images/saved?id=#{aBean.patient.id}" onmouseout="hideLarge()"
									style="border-width: 1px; border-style:solid; border-color:black;"
									onmouseover="showLarge(event,this,'#{request.contextPath}/bss/images/saved?id=#{aBean.patient.id}');" />
							</rich:column>
							<rich:column>
								<f:facet name="header">Name</f:facet>
								<h:outputText value="#{aBean.patient.firstName} #{aBean.patient.lastName} #{aBean.patient.middleName} " />
							</rich:column>
							<rich:column>
								<f:facet name="header">CheckIn Date</f:facet>
								<h:outputText value="#{aBean.dateOfAdmission}">
									<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="#{preferences.timeZone.ID}" />
								</h:outputText>
							</rich:column>
							<rich:column>
								<f:facet name="header">CheckOut Date</f:facet>
								<h:outputText value="#{aBean.dateOfExit}">
									<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="#{preferences.timeZone.ID}"/>
								</h:outputText>								
							</rich:column>														
							<f:facet name="footer">
								<h:outputText value="Total (#{admissionController.exitedAdmissions.rowCount}) Patients checked-out " />
							</f:facet>
						</rich:dataTable>
										

			<img id="imageEnlarged" src="" width="320" height="240" alt="No Image"
				style="position: absolute;  visibility: hidden; border-width: 2px; border-style: solid; border-color: red;" />
									

		</h:form>
		
		<rich:modalPanel id="checkOutWindow" minWidth="300" zindex="2000" resizeable="false" autosized="true" >
			<f:facet name="header">
				<h:outputText value="Refunds"/>
			</f:facet>		
			<h:form>
				<h:panelGrid columns="4">
					<h:outputLabel value="Security Deposit: "/>
					<h:outputText value="#{admissionController.securityDeposit}"/>
					<h:outputLabel value="Security Refund:  "/>
					<h:inputText value="#{admissionController.securityRefund}" size="5" style="border-color:red;border-width:3px; "/>
					<h:outputLabel value="Utensil Deposit : " />
					<h:outputText value="#{admissionController.utensilDeposit}"/>
					<h:outputLabel value="Utensil Refund:  "/>
					<h:inputText value="#{admissionController.utensilRefund}"  size="5"  style="border-color:red;border-width:3px; "/>					
				</h:panelGrid>
				<div align="center">
				<h:panelGrid columns="2">
				<a4j:commandButton onclick="showLoading();"
											title="Click to checkout"
											value="Refund and Check Out"	
											reRender="selectedPatient,exitedAdmissions"										
											oncomplete="hideLoading();if(#{admissionController.error})alert('Error: #{admissionController.errors[0]}, Contact administrator');else { alert('Checkout completed !');} Richfaces.hideModalPanel('checkOutWindow'); selectPage('exit.jsf');"
											action="#{admissionController.startCheckoutNew}" 
											styleClass="norender" />
				<a4j:commandButton 
											title="Click to cancel checkout"
											value="Cancel"									
											onclick="Richfaces.hideModalPanel('checkOutWindow'); return false;" 
											styleClass="norender" />
				</h:panelGrid>
				</div>
			</h:form>
		</rich:modalPanel>	
	</ui:define>
</ui:composition>